//
//  WZretweetView.swift
//  weibo
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit
class WZretweetView: UIView {
    
    var constraint: Constraint?
    
    var cellViewModel: WZCellViewModel?{
        didSet{
            
            retweetLable.text = cellViewModel?.retweetString
            
            // 判断转发微博是否有配图
            
//            清空约束
            constraint?.uninstall()
            if let pictures = cellViewModel?.model?.retweeted_status?.pic_urls where pictures.count > 0{//有配图
                
            self.snp_updateConstraints(closure: { (make) -> Void in
                self.constraint = make.bottom.equalTo(retweetPicture.snp_bottom).constraint
            })
                retweetPicture.hidden = false
                
                var temArray = [WZCellViewModel]()
                for pictureModel in pictures{
                    
                    let cellsViewModel = WZCellViewModel()

                    cellsViewModel.pictureViewModel = pictureModel
                    
                    temArray.append(cellsViewModel)
                }
                retweetPicture.pictureList = temArray
                
                
                
            }else{//没有配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.bottom.equalTo(retweetLable.snp_bottom).constraint
                })
                retweetPicture.hidden = true
            }
            
            
            
            
            
            
        }
    }
    

    
    
    //    MARK:实现初始化方法
   override init(frame: CGRect) {
        super.init(frame: frame)

    setupUI()
//    print(self.subviews)`1                                                                                                  
    }

   required init?(coder aDecoder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
    
    private func setupUI(){
        
        addSubview(retweetLable)
        addSubview(retweetPicture)
//        设置约束
        retweetLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
            //            设置配图的约束
            
            retweetPicture.snp_makeConstraints(closure: { (make) -> Void in
                make.left.equalTo(self.snp_left)
                make.top.equalTo(retweetLable.snp_bottom)
                //给它一个固定的宽高
//                make.size.equalTo(CGSizeMake(40, 40))
            })
//            设置转发微博View的底部约束
            self.snp_makeConstraints(closure: { (make) -> Void in
                self.constraint = make.bottom.equalTo(retweetLable.snp_bottom).constraint
            })
            
        }
        
    }
    
    
    lazy var retweetLable: UILabel = {
    let lable = UILabel()
        lable.text = "就啊圣诞卡和可视电话喀什的卡啊叫"
        lable.font = UIFont.systemFontOfSize(14)
        lable.numberOfLines = 0
        return lable
 
    }()
    lazy var retweetPicture: WZPictureView = {
        let image = WZPictureView()
        return image
    }()
}
